<?php

/** 
 * @author song
 * 
 * 
 */
class CMongoDB {
	
	public $config;
	public $_db;
	public $savaOption;
	
	function __construct($dbConfig) {
		$this->config = $dbConfig;
		$this->savaOption = array ('fsync' => $dbConfig['fsyncFlag'], 
									'safe' => $dbConfig['safeFlag'] );
				
	}
	public static function getInstance($dbConfig){
		return new MongoDB($dbConfig);
	}
	public function getDb(){
		if($this->_db === null){
			$this->_db  = new Mongo($this->config['connectionString'], array(
						'connect'=>true,
					));
					
			$this->_db = $this->_db ->selectDB($this->config['dbName']);
		}
		return $this->_db;
	}
	
	
	public function save($collection,$raw,$option = array()){
		echo $this->saveOption;
		$option = $this->savaOption+ $option;
    
		return $this->db->{$collection}->save($raw,$option);
		
	}
	
	
	public function __get($name)
	{
		$getter='get'.$name;
		if(method_exists($this,$getter))
			return $this->$getter();
		else {
			return  $this->db->{$name};
		}
	}
}

?>